package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import negocio.CommandFacade;
import negocio.ConstantesComando;
import negocio.NegocioException;
import dao.DaoException;
import dominio.Usuario;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");

		Usuario usuario = new Usuario();
		usuario.setLogin(login);
		usuario.setSenha(senha);

		CommandFacade c = new CommandFacade();
		c.setCmd(ConstantesComando.LOGIN);
		try {
			c.execute(usuario, request);
			response.getWriter().print("sucesso");
		} catch (NegocioException e) {
			response.getWriter().print("erro_usuario");
		} catch (DaoException e) {
			response.getWriter().print("erro_banco");
			e.printStackTrace();
		} finally {
			response.getWriter().flush();
			response.getWriter().close();
		}
	}

}
